package interactions
{
	import flash.utils.*;
	
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	import enemies.*;
	import states.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class Interaction extends Entity
	{
		protected var _showSpacebar:Boolean;
		protected var _cooldownBar:FlxBar;
		protected var _cooldown:int;
		
		private var _spacebar:FlxSprite;
		private var _spacebarText:FlxText;
		
		public function Interaction(i:uint = 0, j:uint = 0)
		{
			initSpacebar();
			
			super(i, j)
			
			_showSpacebar = true;
			initCooldownBar();
			
			toggleLight();
		}
		
		override public function update():void
		{
			super.update();
			
			_spacebar.visible = false;
			_spacebarText.visible = false;
			_cooldownBar.visible = false;
			
			_cooldown = _timer.secondsRemaining + 1;
		}
		
		public function get cooldown():int
		{
			return _cooldown;
		}
		
		public function overlapWithEnemy(e:Enemy):void
		{
			// Override this function
		}
		
		public function overlapWithPlayer(p:Player):void
		{
			// Override this function
			if (_showSpacebar)
			{
				_spacebar.visible = true;
				_spacebarText.visible = true;
			}
			
			if (FlxG.keys.justPressed("SPACE"))
			{
				interactWithPlayer(p);
				Tracker.interactionsUsed++;
				logUsage();
			}
		}
		
		public function logUsage():void
		{
			if (FlxG.state is GameState)
			{
				var interactionName:String = getQualifiedClassName(this).slice(14);
				LoggerWrapper.logAction(Registry.actionMap[interactionName], interactionName);
			}
		}
		
		override public function translateToTileByIndex(i:uint, j:uint):void
		{
			super.translateToTileByIndex(i, j);
			translateSpaceBar();
		}
		
		protected function interactWithPlayer(p:Player):void
		{
			// Override this function
		}
		
		private function initSpacebar():void
		{
			_spacebar = new FlxSprite(0, 0);
			_spacebar.loadGraphic(Assets.ImgButtonHorizontal, true, false, 80, 20);
			_spacebar.addAnimation("pressed", [0, 2], 6);
			_spacebar.play("pressed");
			_spacebar.scale.make(0.5, 0.5);
			_spacebar.width /= 2;
			_spacebar.height /= 2;
			_spacebar.centerOffsets();
			_spacebar.visible = false;
			Registry.animations.add(_spacebar);
			
			_spacebarText = new FlxText(x, y, FlxG.width, "S P A C E");
			_spacebarText.size = 6;
			_spacebarText.color = 0xFF000000;
			_spacebarText.visible = false;
			_spacebarText.alignment = "center";
			
			Registry.animations.add(_spacebarText);
			
			translateSpaceBar();
		}
		
		private function translateSpaceBar():void
		{
			_spacebar.x = x - (_spacebar.width / 2) + (Maze.tileWidth / 2);
			_spacebar.y = y + (Maze.tileHeight * 5 / 4);
			
			_spacebarText.x = x - (_spacebarText.width / 2) + (Maze.tileWidth / 2) - 1;
			_spacebarText.y = y + (Maze.tileHeight * 5 / 4);
		}
		
		private function initCooldownBar():void
		{
			_cooldownBar = new FlxBar(0, 0, FlxBar.FILL_LEFT_TO_RIGHT, 25, 5);
			_cooldownBar.setParent(this, "cooldown", true, 0, -Maze.tileHeight / 2);
			_cooldownBar.visible = false;
			Registry.animations.add(_cooldownBar);
		}
	}
}